package cyy.example.com.facotry.net.helper;

import android.text.TextUtils;

import com.example.com.common.factory.data.DataSource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.request.SendTaskEntity;
import cyy.example.com.facotry.model.api.request.UpReportEntity;
import cyy.example.com.facotry.model.api.response.TaskInfoEntity;
import cyy.example.com.facotry.net.Network;
import cyy.example.com.facotry.net.RemoteService;
import cyy.example.com.facotry.net.RequestIdFactory;
import cyy.example.com.facotry.share.SPEngine;
import retrofit2.Call;

/**
 * @author ChenYiYao
 * @date 2018/5/14
 * des : 任务模块请求工具类
 */
public class TaskNetHelper {

    /**
     * 获取任务列表
     *
     * @param pageSize
     * @param pageNum
     * @param callback
     * @return
     */
    public static Call getTaskList(String pageSize, String pageNum, DataSource.Callback callback) {
//        RemoteService remote = Network.remote();
//        Call call = remote.getSearchFirmById(condition);
//        call.enqueue(new RspCallback(callback, RequestIdFactory.SEARCH__FIRM_NAME_ID));
        RemoteService remote = Network.remote();
        Call<BaseEntity<TaskInfoEntity>> call = remote.getTaskList(pageSize, pageNum);
        call.enqueue(new RspCallback<BaseEntity<TaskInfoEntity>>(callback, RequestIdFactory.GET_TASK_LIST));
        return call;
    }

    /**
     * 获取任务列表
     *
     * @param pageSize
     * @param pageNum
     * @param callback
     * @return
     */
    public static Call getOverdueTask(String pageSize, String pageNum, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call<BaseEntity<TaskInfoEntity>> call = remote.getOverdueTask(pageSize, pageNum);
        call.enqueue(new RspCallback<BaseEntity<TaskInfoEntity>>(callback, RequestIdFactory.GET_OVERDUE_TASK));
        return call;
    }


    /**
     * 获取用户分组
     *
     * @param callback
     * @return
     */
    public static Call getTaskUserGroup(DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        SPEngine.UserInfo userInfo = SPEngine.getSPEngine().getUserInfo();
        Call call = remote.getTaskUserGroup(userInfo.getRegionCode(), "1");
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.GET_USER_TASK_GROUP));
        return call;
    }

    /**
     * 接收任务
     *
     * @param callback
     * @return
     */
    public static Call doTakeTask(String Tasknumb, String TaskID, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Map map = new HashMap();
        map.put("taskNumber", Tasknumb);
        map.put("param1", SPEngine.getSPEngine().getUserInfo().getUser_id());
        map.put("taskId", TaskID);
        Call call = remote.doTakeTask(map);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.ACCEPT_TASK));
        return call;
    }

    /**
     * 派发任务
     *
     * @return
     */
    public static Call SendTask(List<String> userId, List<String> tastObjectId, long endTime, String taskName,
                                String taskLevel, String taskTyep, String taskContents, DataSource.Callback callback) {
        StringBuffer buffer = new StringBuffer();
        for (String id : userId) {
            buffer.append(id + ",");
        }
        SPEngine.UserInfo userInfo = SPEngine.getSPEngine().getUserInfo();
        SendTaskEntity entity = new SendTaskEntity(buffer.toString(), endTime, tastObjectId, taskName,
          taskTyep,taskLevel, taskContents, userInfo.getUser_id());
        RemoteService remote = Network.remote();
        Call call = remote.SendTask(entity);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.SEND_TASK));
        return call;
    }

    /**
     * 获取任务表单
     *
     * @param formCode 8唯一位随机码
     * @return
     */
    public static Call getTaskReport(String formCode, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getTaskReport(formCode);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.GET_TASK_REPORT));
        return call;
    }

    /**
     * 获取任务表单
     *
     * @param taskNumb 任务编号
     * @return
     */
    public static Call getTaskReportList(String taskNumb, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getTaskReportList(taskNumb);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.GET_TASK_REPORT));
        return call;
    }

    /**
     * 获取任务表单
     *
     * @param taskNumber 任务编号
     * @return
     */
    public static Call cleanReportResult(String taskNumber, String formCode, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Map<String, String> map = new HashMap<>();
        map.put("taskNumber", taskNumber);
        map.put("formCode", formCode);
        Call call = remote.cleanTaskReportResult(map);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.CLEAN_TASK_REPORT_RESULT));
        return call;
    }

    /**
     * 获取已填写的报表详情
     *
     * @param taskNumber 任务编号
     * @return
     */
    public static Call getReportAndValue(String taskNumber, String formCode, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getReportDetlAndValue(taskNumber, formCode);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.GET_REPORT_AND_VALUE));
        return call;
    }

    /**
     * 获取已填写的报表详情
     *
     * @param taskNumber 任务编号
     * @return
     */

    /**
     * 上传表格
     *
     * @param entity 上传表格的实体
     * @return
     */
    public static Call UpLoadReport(UpReportEntity entity, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.upLoadReport(entity);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.UP_LOAD_REPORT));
        return call;
    }

    /**
     * 更新表格
     *
     * @param entity 上传表格的实体
     * @return
     */
    public static Call UpDataReport(UpReportEntity entity, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.upDataReport(entity);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.UP_DATA_REPORT));
        return call;
    }

    /**
     * 更新表格
     *
     * @param taskNumb 上传表格的实体
     * @return
     */
    public static Call getTaskFileList(String taskNumb, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getTaskFiles(taskNumb);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.GET_TASK_FILE));
        return call;
    }

    /**
     * 更新表格
     *
     * @param taskNumb 上传表格的实体
     * @return
     */
    public static Call getTaskFileAndReport(String taskNumb, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getTaskFilesAndReport(taskNumb);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.GET_TASK_FILE_REPORT));
        return call;
    }

    /**
     * 提交审核
     *
     * @param taskNumb 上传表格的实体
     * @return
     */
    public static Call doSubmitAudit(String taskNumb, String taskId, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("taskNumber", taskNumb);
        stringMap.put("taskId", taskId);
        Call call = remote.doSubmitAudit(stringMap);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.SUBMIT_TASK_AUDIT));
        return call;
    }

    /**
     * 审核通过
     *
     * @param taskNumb 上传表格的实体
     * @return
     */
    public static Call taskAuditPass(String taskNumb, String taskId, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("taskNumber", taskNumb);
        stringMap.put("taskId", taskId);
        Call call = remote.taskAuditPass(stringMap);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.TASK_AUDIT_PASS));
        return call;
    }

    /**
     * 审核驳回
     *
     * @param taskNumb 上传表格的实体
     * @return
     */
    public static Call taskAuditRejct(String taskNumb, String taskId, String remark, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("taskNumber", taskNumb);
        stringMap.put("remark", remark);
        stringMap.put("taskId", taskId);
        Call call = remote.taskAuditReject(stringMap);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.TASK_AUDIT_REJECT));
        return call;
    }

    /**
     * 查询任务详情
     *
     * @param taskNumb       任务id
     * @param histologicType 组织类似编号
     * @param callback
     * @return
     */
    public static Call getTaskDetailsInfo(String taskNumb, String histologicType, DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Call call = remote.getTaskDetailsInfo(taskNumb, histologicType);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.GET_TASK_DETAILS_INFO));
        return call;
    }

    /**
     *
     * @param taskNumber 任务编号
     * @param taskId 任务id
     * @param taskType 任务类型
     * @param taskObjects 任务对象的id
     * @param taskFromStatus 任务状态
     * @param callback
     * @return
     */
    public static Call getDeleteTask(String taskNumber, String taskId,String taskType , String taskObjects ,String taskFromStatus , DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Map<String, String> map = new HashMap<>();
        map.put("taskNumber", taskNumber);
        map.put("taskId", taskId);
        map.put("taskType", taskType);
        map.put("taskObjects", taskObjects);
        map.put("taskFromStatus", taskFromStatus);
        Call call = remote.deleteTaskTask(map);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.DELELTE_TASK));
        return call;
    }

    /**
     * @param taskToIds    执行人id
     * @param endTime      结束时间
     * @param taskName     任务名称
     * @param taskDegree   任务等级
     * @param taskType     任务类型
     * @param taskContents 任务内容
     * @param taskNumber   任务编码
     * @param taskId       任务id
     * @return
     */
    public static Call alterNoAcceptTask(String taskToIds, long endTime, String taskName, String taskDegree,
                                         String taskType, String taskContents, String taskNumber, String taskId,
                                         DataSource.Callback callback) {
        RemoteService remote = Network.remote();
        Map<String, String> map = new HashMap<>();
        map.put("taskToIds", taskToIds);
        map.put("endTime", endTime + "");
        map.put("taskName", taskName);
        map.put("taskDegree", taskDegree);
        map.put("taskType", taskType);
        if (!TextUtils.isEmpty(taskContents)) {
            map.put("taskContents", taskContents);
        }
        map.put("taskNumber", taskNumber);
        map.put("taskId", taskId);
        Call call = remote.alterNoAcceptTask(map);
        call.enqueue(new RspCallback<BaseEntity>(callback, RequestIdFactory.ALTER_TASK_INFO));
        return call;
    }


}
